#include<string>
#include<vector>
using namespace std;

class Solution {
private:
    bool isSubsequence(const string& s, const string& t) {
        int j = 0;
        for (int i = 0; i < t.size()&&j< s.size(); ++i) {
            if (s[j] == t[i]) {
                ++j;
            }
        }
        return j == s.size();
    }

public:
    string findLongestWord(string s, vector<string>& d) {
        int maxLen = 0;
        string res = "";
        for (auto& str : d) {
            if (isSubsequence(str, s)) {
                if (maxLen < str.length() || (maxLen == str.length() && res > str)) {
                    res = str;
                    maxLen = res.length();
                }
            }
        }
        return res;
    }
};